//封装了请求对象
//import storage  from '@/com/storage.js'
import app from "@/main.js"
function sendBaseUniPostRequest(uid, token, msgType, data, addr) {
    return new Promise((resolve, reject) => { // 异步封装接口，使用 Promise 处理异步请求
        // 创建请求头部
        const headers = {
            'content-type': 'application/json',
        };

        // 仅在 uid 和 token 不为 null 时添加自定义请求头
        if (uid) {
            headers['uid'] = uid;
        }
        if (token) {
            headers['token'] = token;
        }
        uni.request({
            url: addr,
            data: {
                data: data,
                msgType: msgType
            },
            method: "POST",
            timeout: 5000,
            header: headers,
            success: (res) => {
                resolve(res.data);
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
}

function sendBaseUniGetRequest(uid,token,addr){
	return new Promise((resolve, reject) => { //异步封装接口，使用Promise处理异步请求
	// 创建请求头部
	const headers = {
	    'content-type': 'application/json',
	};
	
	// 仅在 uid 和 token 不为 null 时添加自定义请求头
	if (uid) {
	    headers['uid'] = uid;
	}
	if (token) {
	    headers['token'] = token;
	}
	   uni.request({
	   		    url: addr,
	   			method:"GET",
	   			timeout:5000,
	   		    header: headers,
	   		    success: (res) => {
	   		       resolve(res.data);
	   		    },
	   			fail: (err) => {
	   			   reject(err);
	   			}
	   	});
	})
}

async function sendUniGetRequestByNetWork(uid, token,addr){
				//1.判断是否可以请求 -----有网即可
				let res=undefined;
				if(app.$store.state.networkStatus){
					//获取信息
					try{
						res=await sendBaseUniGetRequest(uid,token,addr);
						
						if(res.status===500){
							uni.showToast({
								title: '服务器异常',
								icon: 'error',  //success 等
								duration: 1000
							});
							res=undefined;
						}
					}catch(e){
						//TODO handle the exception
						uni.showToast({
							title: '服务异常',
							icon: 'error',  //success 等
							duration: 1000
						});
						res=undefined;
					}	
				}else{
					uni.showToast({
						title: '网络异常',
						icon: 'error',  //success 等
						duration: 1000
					});
				}
				return res;
}








async function sendUniPostRequestByNetWork(uid, token, msgType,data,virtualDir){
				//1.判断是否可以请求 -----有网即可
				let res=undefined;
				if(app.$store.state.networkStatus){
					//获取信息
					try{
						let url=app.baseUrl+virtualDir;
						console.log(url);
						res=await sendBaseUniPostRequest(uid,token,msgType,data,url);
						if(res.status===500){
							uni.showToast({
								title: '服务器异常',
								icon: 'error',  //success 等
								duration: 1000
							});
							res=undefined;
						}
					}catch(e){
						//TODO handle the exception
						uni.showToast({
							title: '服务异常',
							icon: 'error',  //success 等
							duration: 1000
						});
						res=undefined;
					}	
				}else{
					uni.showToast({
						title: '网络异常',
						icon: 'error',  //success 等
						duration: 1000
					});
				}
				return res;
}

module.exports ={sendBaseUniPostRequest,sendUniPostRequestByNetWork,sendBaseUniGetRequest,sendUniGetRequestByNetWork};